<?php
/**
 * Created by PhpStorm.
 * User: Talent Gao
 * Date: 14-8-28
 * Time: 下午3:14
 */
class CommonModel extends Db_Base{
    private $_IndexMarkInfo;

    /**
     *__construct
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * 获取原油指数，复制原来
     * @return array
     */
    public function getIndex(){
        $ch = curl_init();
        $url = "http://www.isuwang.com/";
        $a1 = "/<div class=\"price fl\">([\d\D]*?)<\/div>/i";
        $a2 = "/<tr>([\d\D]*?)<\/tr>/i";
        $a3 = "/<td>([\d\D]*?)<\/td>/i";
        $a4_1 = "/<tdclass=\"greenchange\">([\d\D]*?)<\/td>/i";
        $a4_2 = "/<tdclass=\"redchange\">([\d\D]*?)<\/td>/i";
        $a4_3 = "/<tdclass=\"bluechange\">([\d\D]*?)<\/td>/i";


        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 0);

        $output = curl_exec($ch);
        curl_close($ch);
        $arr1=array();
        preg_match_all($a1,$output,$arr1);
        if(!empty($arr1[1])){
            $a=$arr1[1][0];
            $b=$arr1[1][1];
        }
        if(!empty($a)){
            $arr1 =preg_replace("/\s|　/","",$a);
        }

        if(!empty($b)){
            $arr2 =preg_replace("/\s|　/","",$b);
        }

        if(!empty($arr1)&&!empty($arr2)){
            preg_match_all($a2,$arr1,$arr2_1);
            preg_match_all($a2,$arr2,$arr2_2);
        }
        if(!empty($arr2_1[1])&&!empty($arr2_2[1])){
            $arr_12 = array_merge($arr2_1[1],$arr2_2[1]);
        }

        if(!empty($arr_12)){
            foreach($arr_12 as $k=>$v){
                preg_match_all($a3,$v,$arr3);
                if(!empty($arr3[1])){
                    $li_1 = $arr3[1][0];
                    $li_2 = $arr3[1][1];
                }
                preg_match_all($a4_1,$v,$arr4_1);
                if(!empty($arr4_1[1])){
                    $li_3 = $arr4_1[1][0];
                    $status = "fall";
                }else{
                    preg_match_all($a4_2,$v,$arr4_2);
                    if(!empty($arr4_2[1])){
                        $li_3 = $arr4_2[1][0];
                        $status = "rise";
                    }else{
                        preg_match_all($a4_3,$v,$arr4_3);
                        if($arr4_3[1]){
                            $li_3 = $arr4_3[1][0];
                            $status = "flat";
                        }

                    }
                }
                $data_info[$k] = array(
                    'name'=>$li_1,
                    'num'=>$li_2,
                    'updown'=>$li_3,
                    'status'=>$status,
                );
            }
        }

        if(!empty($data_info)){
            $li_ = $this->_db_handler->fetchAll("select  * from cfg_zhishu");
            if($li_){
                foreach($li_ as $k=>$v){
                    $this->_db_handler->update("cfg_zhishu",$data_info[$k],"id = {$li_[$k]['id']}");
                }
            }else{
                foreach($data_info as $k=>$v){
                    $this->_db_handler->insert("cfg_zhishu",$v);
                }
                $li_ = $this->_db_handler->fetchAll("select  * from cfg_zhishu");
            }
        }else{
            $li_ = $this->_db_handler->fetchAll("select  * from cfg_zhishu");
        }

        return $li_;
    }

    /**
     * 获取交易类型
     * @param $where
     * @return array
     */
    public function getService($where){
        if(empty($where)){
            $where = "1 = 1";
        }
        $data = $this->_db_handler->fetchRow("select  * from res_service where {$where}");
        return $data;
    }

    public function getService_info($where){
        if(empty($where)){
            $where = "1 = 1";
        }
        $data = $this->_db_handler->fetchAll("select  * from res_service where {$where}");
        return $data;
    }

    /**
     * 获取币种
     * @param $where
     * @return array
     */
    public function getCurrency($where){
        if(empty($where)){
            $where = "1 = 1";
        }
        $data = $this->_db_handler->fetchRow("select  * from res_currency where {$where}");
        return $data;
    }

    /**
     * 获取地区
     * @param $where
     * @return array
     */
    public function getArea($where){
        if(empty($where)){
            $where = "1 = 1";
        }
        $data = $this->_db_handler->fetchRow("select  * from region where {$where}");
        return $data;
    }

    /**
     * 获得省，市地区
     * @param $where
     * @return array
     */
    public function getParentArea($where){
        if(empty($where)){
            $where = "1 = 1";
        }
        $data = $this->_db_handler->fetchAll("select  * from region where {$where}");
        return $data;
    }

    /**
     * 首页轮播图
     * @param $where
     * @return array
     */
    public function getCarouselByWhere($where){
        if(empty($where)){
            $where = "1 = 1";
        }

        $data = $this->_db_handler->fetchAll("select  c.* ,a.name as activity_name from carousel as c left join activity as a on(a.id = c.activity_id) where {$where} order by c.order_by asc");
        return $data;
    }

    /**
     * @param $data
     * @return bool|int
     */
    public function addSiteLog($data){
        if(empty($data)){
            return false;
        }
        return $this->_db_handler->insert("site_log",$data);
    }

}